package com.gongpingjialoan.adapter.car;


import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.category.PinnedHeaderListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class CarModelDetailAdapter extends BaseAdapter implements PinnedHeaderListView.PinnedHeaderAdapter,
    ListView.OnScrollListener
{
    
    public LayoutInflater mInflater;
    
    JSONArray jsa;
    
    Context context;
    
    int currentPosition = -1;
    
    public CarModelDetailAdapter(Context context)
    {
        this.context = context;
        mInflater = LayoutInflater.from(context);
    }
    
    public void setData(JSONArray jsa)
    {
        this.jsa = jsa;
        notifyDataSetChanged();
    }
    
    public void setCurrentPostion(int position)
    {
        this.currentPosition = position;
        notifyDataSetChanged();
    }
    
    @Override
    public int getCount()
    {
        if (jsa == null)
        {
            return 0;
        }
        return jsa.length();
        
    }
    
    @Override
    public JSONObject getItem(int position)
    {
        try
        {
            return jsa.getJSONObject(position);
        }
        catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    @Override
    public long getItemId(int position)
    {
        
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ModelViewHolder holder;
        
        if (convertView == null)
        {
            convertView = mInflater.inflate(R.layout.item_list_model_detail, null);
            holder = new ModelViewHolder();
            holder.modelText = (TextView)convertView.findViewById(R.id.modelText);
            holder.modelYear = (TextView)convertView.findViewById(R.id.title);
            holder.priceT = (TextView)convertView.findViewById(R.id.price);
            holder.desT = (TextView)convertView.findViewById(R.id.des);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ModelViewHolder)convertView.getTag();
        }
        holder.modelText.setTextColor(currentPosition == position ? context.getResources()
            .getColor(R.color.nav_bg_color) : context.getResources().getColor(R.color.text_title_color));
        
        JSONObject jo;
        try
        {
            jo = jsa.getJSONObject(position);
            holder.modelText.setText(jo.getString("detail_model"));
            String year = jo.getString("year");
            holder.priceT.setText("￥" + jo.getString("price_bn") + "万");
            if (needTitle(position))
            {
                holder.modelYear.setVisibility(View.VISIBLE);
                holder.desT.setVisibility(View.VISIBLE);
                holder.modelYear.setText(year);
            }
            else
            {
                holder.modelYear.setVisibility(View.GONE);
                holder.desT.setVisibility(View.GONE);
            }
        }
        catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return convertView;
        
    }
    
    @Override
    public int getPinnedHeaderState(int position)
    {
        if (getCount() == 0 || position < 0)
        {
            return PinnedHeaderListView.PinnedHeaderAdapter.PINNED_HEADER_GONE;
        }
        
        try
        {
            if (isMove(position))
            {
                return PinnedHeaderListView.PinnedHeaderAdapter.PINNED_HEADER_PUSHED_UP;
            }
        }
        catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return PinnedHeaderListView.PinnedHeaderAdapter.PINNED_HEADER_VISIBLE;
    }
    
    @Override
    public void configurePinnedHeader(View headerView, int position, int alpaha)
    {
        String currLetter = null;
        try
        {
            currLetter = jsa.getJSONObject(position).getString("year");
        }
        catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(currLetter))
        {
            TextView headerTextView = (TextView)headerView.findViewById(R.id.header);
            headerTextView.setText(currLetter);
        }
        
    }
    
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
    {
        
        if (view instanceof PinnedHeaderListView)
        {
            ((PinnedHeaderListView)view).controlPinnedHeader(firstVisibleItem);
        }
        
    }
    
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState)
    {
        
    }
    
    // ===========================================================
    // Methods
    // ===========================================================
    
    private boolean needTitle(int position)
    {
        if (position == 0)
        {
            return true;
        }
        
        if (position < 0)
        {
            return false;
        }
        
        String currentTitle = null;
        try
        {
            currentTitle = jsa.getJSONObject(position).getString("year");
        }
        catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String previousTitle = null;
        try
        {
            previousTitle = jsa.getJSONObject(position - 1).getString("year");
        }
        catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (null == currentTitle || null == previousTitle)
        {
            return false;
        }
        
        return !currentTitle.equals(previousTitle);
    }
    
    private boolean isMove(int position)
        throws JSONException
    {
        String currentTitle = jsa.getJSONObject(position).getString("year");
        if ((position + 1) >= jsa.length())
        {
            return false;
        }
        String nextTitle = jsa.getJSONObject(position + 1).getString("year");
        if (null == currentTitle || null == nextTitle)
        {
            return false;
        }
        
        return !currentTitle.equals(nextTitle);
    }
    
    public final class ModelViewHolder
    {
        public TextView modelText;
        
        public TextView modelYear;
        
        public TextView priceT;
        
        public TextView desT;
    }
    
}
